﻿using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.EntityFrameworkCore;
using TrackSystem.Entities.Models;

namespace TrackSystem.EntityFramework.Mapping
{
    public class SensorMap : IEntityTypeConfiguration<SensorTable>
    {
        public void Configure(EntityTypeBuilder<SensorTable> builder)
        {
            builder.ToTable("Sensor");

            builder.HasKey(s => s.Id);
            builder.HasIndex(s => s.Id).IsUnique();

            builder.Property(s => s.Name).HasMaxLength(50).IsRequired();
            builder.HasIndex(s => s.Name).IsUnique();

            builder.Property(s => s.Description).HasMaxLength(200);

            builder.HasOne(s  => s.Channel)
                   .WithMany(c => c.Sensors)
                   .HasForeignKey(s => s.channelID)
                   .OnDelete(DeleteBehavior.Cascade);

            builder.HasOne(s => s.Equip)
                .WithMany(e => e.Sensors)
                .HasForeignKey(s => s.equipId)
                .OnDelete(DeleteBehavior.NoAction);
        }
    }
}
